package com.why.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.entity.*;
import com.why.service.*;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 教师(Teachers)表控制层
 *
 * @author makejava
 * @since 2023-04-23 16:05:40
 */
@RestController
@RequestMapping("/teacher")
@CrossOrigin
public class TeachersController {
    /**
     * 服务对象
     */

    @Autowired
    private TeachersService teachersService;
    @Autowired
    private KechengxinxiService kechengxinxiService;

    @Autowired
    private CourseselectionService courseselectionService;

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private ExaminationService examinationService;

    @PostMapping("/login")
    public String Login(HttpServletRequest request, @RequestBody Teachers teacher) {

        LambdaQueryWrapper<Teachers> queryWrapper = new LambdaQueryWrapper<Teachers>();
        queryWrapper.eq(Teachers::getJiaoshigonghao, teacher.getJiaoshigonghao()).eq(Teachers::getMima, teacher.getMima());
        Teachers user = teachersService.getOne(queryWrapper);
        System.out.println(user);
        if (user == null) {
            return "error";
        } else {
            request.getSession().setAttribute("teacherId", user.getId());
            return "success";
        }
    }

    @PostMapping("/logout")
    public String Logout(HttpServletRequest request) {
        request.getSession().removeAttribute("teacherId");
        return "success";
    }

    /**
     * 获取登录admin的信息
     *
     * @return
     */
    @GetMapping("/getteacher")
    public Teachers getAdmin(HttpServletRequest request) {
        Long id = (Long) request.getSession().getAttribute("teacherId");
        LambdaQueryWrapper<Teachers> queryWrapper = new LambdaQueryWrapper<Teachers>();
        queryWrapper.eq(Teachers::getId, id);
        Teachers teacher = teachersService.getOne(queryWrapper);
        return teacher;
    }

    /**
     * 新增教师
     *
     * @param teacher
     * @return
     */
    @PostMapping("/save")
    public String save(@RequestBody Teachers teacher) {
        teachersService.save(teacher);
        System.out.println(teacher);
        return "success";
    }


    /**
     * 修改个人信息
     *
     * @param teacher
     * @param request
     * @return
     */
    @PostMapping("/update")
    public String update(@RequestBody Teachers teacher, HttpServletRequest request) {
//        Long teacherId = (Long) request.getSession().getAttribute("teacherId");
        Long teacherId = teacher.getId();
        LambdaQueryWrapper<Teachers> queryWrapper = new LambdaQueryWrapper<Teachers>();
        queryWrapper.eq(Teachers::getId, teacherId);
        teachersService.update(teacher, queryWrapper);
        return "success";
    }

    /**
     * 分页查询教师信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Page<Teachers> page(int page, int pageSize, String name) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Teachers> queryWrapper = new LambdaQueryWrapper<Teachers>();
        queryWrapper.like( Teachers::getJiaoshixingming, name);
        queryWrapper.orderByDesc(Teachers::getId);
        teachersService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    /**
     * 根据id删除教师信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        teachersService.removeById(id);
        return "success";
    }

    /**
     * 查询对应教师的授课信息
     * 课程信息表中查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/teacherCourse/page")
    public Page<Kechengxinxi> page1(int page, int pageSize, String name, HttpServletRequest request) {
        Page pageInfo = new Page(page, pageSize);
        Long teacherId = (Long) request.getSession().getAttribute("teacherId");
        LambdaQueryWrapper<Teachers> queryWrapper1 = new LambdaQueryWrapper<Teachers>();
        queryWrapper1.eq(Teachers::getId, teacherId);
        String jiaoshigonghao = teachersService.getOne(queryWrapper1).getJiaoshigonghao();
        LambdaQueryWrapper<Kechengxinxi> queryWrapper = new LambdaQueryWrapper<Kechengxinxi>();
        queryWrapper.like(name != null, Kechengxinxi::getKechengmingcheng, name);
        queryWrapper.orderByDesc(Kechengxinxi::getAddtime);
        queryWrapper.eq(Kechengxinxi::getJiaoshigonghao, jiaoshigonghao);
        kechengxinxiService.page(pageInfo, queryWrapper);
        return pageInfo;
    }


    /**
     * 根据教师工号分页查询学生选课信息
     * 选课信息表中查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @param request
     * @return
     */
    @GetMapping("/select/page")
    public Page<Courseselection> page2(int page, int pageSize, String name, HttpServletRequest request) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Courseselection> queryWrapper = new LambdaQueryWrapper<Courseselection>();
        LambdaQueryWrapper<Teachers> queryWrapper1 = new LambdaQueryWrapper<Teachers>();
        Long id = (Long) request.getSession().getAttribute("teacherId");
        queryWrapper1.eq(Teachers::getId, id);
        Teachers teacher = teachersService.getOne(queryWrapper1);
        String gonghao = teacher.getJiaoshigonghao();
        queryWrapper.like(Courseselection::getXueshengxingming, name);
        queryWrapper.eq(Courseselection::getJiaoshigonghao, gonghao);
        queryWrapper.orderByDesc(Courseselection::getAddtime);
        courseselectionService.page(pageInfo, queryWrapper);
        return pageInfo;
    }


    /**
     * 根据教师Id查询相应的订单信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @param request
     * @return
     */
    @GetMapping("/order/page")
    public Page<Orders> page3(int page, int pageSize, String name, HttpServletRequest request) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<Orders>();
        //获取教师工号
        LambdaQueryWrapper<Teachers> queryWrapper1 = new LambdaQueryWrapper<Teachers>();
        Long id = (Long) request.getSession().getAttribute("teacherId");
        queryWrapper1.eq(Teachers::getId, id);
        Teachers teacher = teachersService.getOne(queryWrapper1);
        String gonghao = teacher.getJiaoshigonghao();
        //分页查询条件
        queryWrapper.like(Orders::getCoursename, name);
        queryWrapper.eq(Orders::getJiaoshigonghao, gonghao);
        queryWrapper.orderByDesc(Orders::getAddtime);

        ordersService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    /**********************************************************************************************************************************
     * 通过教师ID获取试卷的分页查询
     * 表为Examination
     *
     * @param page
     * @param pageSize
     * @param name
     * @param request
     * @return
     */
    @GetMapping("/examination/page")
    public Page<Examination> page4(int page, int pageSize, String name, HttpServletRequest request) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Examination> queryWrapper = new LambdaQueryWrapper<Examination>();
        //获取教师工号
        Long id = (Long) request.getSession().getAttribute("teacherId");
        queryWrapper.like(Examination::getExamname, name);
        queryWrapper.eq(Examination::getTeacherid, id);
        examinationService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    @PostMapping("/saveexam")
    private String saveExam(@RequestBody Examination examination, HttpServletRequest request) {
        LambdaQueryWrapper<Examination> queryWrapper = new LambdaQueryWrapper<Examination>();
        LambdaQueryWrapper<Teachers> queryWrapper1 = new LambdaQueryWrapper<Teachers>();
        LambdaQueryWrapper<Examination> queryWrapper2 = new LambdaQueryWrapper<Examination>();
        //获取教师工号
        Long id = (Long) request.getSession().getAttribute("teacherId");
        queryWrapper1.eq(Teachers::getId, id);
        Teachers teacher = teachersService.getOne(queryWrapper1);
        System.out.println(teacher);

        examination.setTeacherid(id);
        examination.setJiaoshigonghao(teacher.getJiaoshigonghao());
        examination.setTeachername(teacher.getJiaoshixingming());

        queryWrapper2.eq(Examination::getExamname, examination.getExamname());
        Examination one = examinationService.getOne(queryWrapper2);
        if (one == null) {
            examinationService.save(examination);
            return "success";
        } else {
            return "exist";
        }
    }

    @PostMapping("/examination/update")
    private String updateExam(@RequestBody Examination examination) {
        LambdaQueryWrapper<Examination> queryWrapper = new LambdaQueryWrapper<Examination>();
        queryWrapper.eq(Examination::getId, examination.getId());
        Examination one = examinationService.getOne(queryWrapper);
        if (one != null) {
            examinationService.update(examination, queryWrapper);
            return "success";
        } else {
            return "error";
        }
    }





}

